package net.googlecode.garzha.controllers;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: 13.06.11
 * Time: 20:42
 * To change this template use File | Settings | File Templates.
 */

import net.googlecode.garzha.domains.Group;
import net.googlecode.garzha.domains.Person;
import net.googlecode.garzha.services.PersonService;
import net.googlecode.garzha.services.SecurityService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

/**
 * Controller setting page.
 */
@Controller
public class SettingController {

    @Autowired
    private PersonService personService;
    @Autowired
    private SecurityService securityService;

    protected static final Logger logger = Logger.getLogger(IndexController.class);


    @RequestMapping("/setting")
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        ModelAndView mv = new ModelAndView("setting");

        logger.info("Setting Get current person");
        Person prs = personService.getPerson(securityService.getCurrentUser());
        Group gr = personService.getGroup(prs.getGrpid());
        mv.addObject("info", prs);
        mv.addObject("group", personService.getListGroup());
        mv.addObject("root", request.getContextPath());

        logger.info("SETTING Returning user page view");
        return mv;
    }

    /**
     * Not ready.
     *
     * @param imgurl
     * @return
     * @throws java.io.IOException
     */
    @RequestMapping(value = "/setpic", method = RequestMethod.POST)
    public String addPic(@RequestParam(value = "imgurl", required = true) MultipartFile imgurl) throws IOException {
        logger.info("Setpic user add function ");

        if (imgurl.getContentType().equals("image/jpeg")) {
            Person prs = personService.getPerson(securityService.getCurrentUser());
            File f = new File("D:/svnprac/src/garzha/PracticeWebProject/src/main/webapp/userimg/" + prs.getId() + ".jpg");
          if (f.exists()) {
                logger.info("exists");
                f.delete();
                f.createNewFile();
            } else {
                f.createNewFile();
            }



            logger.info(imgurl.getContentType());
            logger.info(imgurl.getOriginalFilename());
            logger.info("Трансфер");

            imgurl.transferTo(f);

            prs.setImgurl(prs.getId() + ".jpg");
            personService.updatePerson(prs);
            logger.info("Setpic return function ");
        }
        return "redirect:/home";
    }

    @RequestMapping(value = "/setfio", method = RequestMethod.POST)
    public String setFIO(@ModelAttribute("person") Person person,
                         BindingResult result) {
        logger.info("setinfo ");
        Person prs = personService.getPerson(securityService.getCurrentUser());
        prs.setFname(person.getFname());
        prs.setSname(person.getSname());
        prs.setTname(person.getTname());
        prs.setGrpid(person.getGrpid());
        personService.updatePerson(prs);
        logger.info("setfio ");
        return "redirect:/home";
    }

    @RequestMapping(value = "/setall", method = RequestMethod.POST)
    public String setPas(@RequestParam(value = "password", required = true) String password,
                         @RequestParam(value = "spas", required = true) String spas
    ) {
        logger.info("pas add function ");
        Person prs = personService.getPerson(securityService.getCurrentUser());
        if (spas.equals(prs.getPassword())) {
            logger.info("pas correct");
            prs.setPassword(password);
        }

        personService.updatePerson(prs);
        logger.info("set return function ");
        return "redirect:/home";
    }

}